from django.shortcuts import render
from django.views import View
from django.http import HttpResponse,JsonResponse,HttpResponseRedirect

from order.models import Order
from reservation.models import Reservation
from users.models import User
from cars.models import Cardetails

# Create your views here.


#支付回调，获取到支付结果
def Checkbuy(request,reservation_id=1):
    dingdan = request.GET.dict()
    order_number = dingdan.pop('trade_no')
    if order_number:
        Order.objects.filter(cars_id=reservation_id).update(order_number=order_number,order_state='已支付')
    return HttpResponseRedirect('/order/')



#展示订单
class OrderView(View):
    def get(self,request):
        user = request.session.get('user')
        order_list = Order.objects.filter(user_id=user.id).order_by('id')
        users = User.objects.get(id=user.id)
        reservation_list = Reservation.objects.filter(user_id=user.id)
        number= len(reservation_list)
        return render(request, 'templates/order/order.html', {'order_list':order_list, 'users':users, 'number':number})

#删除订单
class DeleteOrderView(View):
    def get(self,request,order_id=1):
        order = Order.objects.get(id=order_id)
        order.delete()
        return HttpResponseRedirect('/order/')

def chat_view(request):
    return render(request,"consumer.html")